//
//  MainVC.m
//  AlumniBook
//
//  Created by 喂！ on 2018/10/30.
//  Copyright © 2018年 well. All rights reserved.
//

#import "MainVC.h"

@interface MainVC ()

@end

@implementation MainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initFrame];
    [self initData];
    [self setTitleName:@"AlumniBook"];
}

-(void)initFrame
{
    MainTabView *tabBarView = [[MainTabView alloc] init];
    tabBarView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tabBarView];
    tabBarView.delegate = self;
    [tabBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(49);
    }];
    
    
    self.homeVC = [HomeVC new];
    [self addChildViewController:self.homeVC];
    [self.view addSubview:self.homeVC.view];
    [self.homeVC.view setFrame:CGRectMake(0, 20 + self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height- (20 + self.navigationController.navigationBar.frame.size.height + 49))];
    
    self.bookVC = [BookVC new];
//    [self addChildViewController:self.bookVC];
//    [self.view addSubview:self.bookVC.view];
    [self.bookVC.view setFrame:self.homeVC.view.frame];
    
    self.userVC = [UserVC new];
//    [self addChildViewController:self.userVC];
//    [self.view addSubview:self.userVC.view];
    [self.userVC.view setFrame:self.homeVC.view.frame];
    
    
    self.messageVC = [MessageVC new];
//    [self addChildViewController:self.messageVC];
//    [self.view addSubview:self.messageVC.view];
    [self.messageVC.view setFrame:self.homeVC.view.frame];

    
}

-(void)initData
{
    self.childArr = @[self.homeVC,self.bookVC,@{},self.messageVC,self.userVC];
}

#pragma mark    MainViewDelegate
-(void)changeChoosen:(NSInteger)tag
{
    NSInteger choosenIndex = tag - 100;
    
    if (choosenIndex == 2) {
        [self choosenFunction];
    }
    else
    {
        if (choosenIndex != self.currentIndex) {
            
            UIViewController *old = [self.childArr objectAtIndex:self.currentIndex];
            UIViewController *new = [self.childArr objectAtIndex:choosenIndex];
            
            [self addChildViewController:new];
            
            [self transitionFromViewController:old
                              toViewController:new
                                      duration:.3f
                                       options:UIViewAnimationOptionTransitionNone
                                    animations:^{
                                        
                                    }
                                    completion:^(BOOL finished) {
                                        if (finished) {
                                            [new didMoveToParentViewController:self];
                                            [old willMoveToParentViewController:nil];
                                            [old removeFromParentViewController];
//                                            [self.view bringSubviewToFront:projectChoosenViewController.view];
                                            
                                            self.currentIndex = choosenIndex;
                                        }
                                    }];
        }
        else if (self.currentIndex == 0)
        {
//            [self choosenProject:self.currentProject];
        }
    }
}

-(void)choosenFunction
{
    UIButton *button = [self.view viewWithTag:102];
    [button setImage:nil forState:UIControlStateNormal];
    
    AlertCustomVC *vc = [[AlertCustomVC alloc] init];
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
//    [vc setFunctionArr:[firstViewController getFastCreateFunc]];
    vc.changeBlock = ^(NSMutableDictionary *choosenDic){
        [button setImage:[UIImage imageNamed:@"CreateTaskDefault"] forState:UIControlStateNormal];


    };
    
    [self presentViewController:vc animated:false completion:nil];
}


#pragma mark    懒加载
-(NSArray *)childArr
{
    if (!_childArr) {
        _childArr = [NSArray array];
    }
    return _childArr;
}

@end
